package cn.iocoder.yudao.module.jikong.controller.admin.backup;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.jikong.controller.admin.backup.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.backup.BackupDO;
import cn.iocoder.yudao.module.jikong.service.backup.BackupService;

@Tag(name = "管理后台 - 数据库备份")
@RestController
@RequestMapping("/jikong/backup")
@Validated
public class BackupController {

    @Resource
    private BackupService backupService;

    @PostMapping("/create")
    @Operation(summary = "创建数据库备份")
    @PreAuthorize("@ss.hasPermission('jikong:backup:create')")
    public CommonResult<Long> createBackup() {
        return success(backupService.createBackup());
    }

    @PostMapping("/restore")
    @Operation(summary = "恢复数据库备份")
    @Parameter(name = "id", description = "备份编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:backup:restore')")
    public CommonResult<Boolean> restoreBackup(@RequestParam("id") Long id) {
        backupService.restoreBackup(id);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据库备份")
    @PreAuthorize("@ss.hasPermission('jikong:backup:update')")
    public CommonResult<Boolean> updateBackup(@Valid @RequestBody BackupSaveReqVO updateReqVO) {
        backupService.updateBackup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据库备份")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('jikong:backup:delete')")
    public CommonResult<Boolean> deleteBackup(@RequestParam("id") Long id) {
        backupService.deleteBackup(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除数据库备份")
                @PreAuthorize("@ss.hasPermission('jikong:backup:delete')")
    public CommonResult<Boolean> deleteBackupList(@RequestParam("ids") List<Long> ids) {
        backupService.deleteBackupListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据库备份")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('jikong:backup:query')")
    public CommonResult<BackupRespVO> getBackup(@RequestParam("id") Long id) {
        BackupDO backup = backupService.getBackup(id);
        return success(BeanUtils.toBean(backup, BackupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据库备份分页")
    @PreAuthorize("@ss.hasPermission('jikong:backup:query')")
    public CommonResult<PageResult<BackupRespVO>> getBackupPage(@Valid BackupPageReqVO pageReqVO) {
        PageResult<BackupDO> pageResult = backupService.getBackupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BackupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据库备份 Excel")
    @PreAuthorize("@ss.hasPermission('jikong:backup:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBackupExcel(@Valid BackupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BackupDO> list = backupService.getBackupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据库备份.xls", "数据", BackupRespVO.class,
                        BeanUtils.toBean(list, BackupRespVO.class));
    }

}